package com.qf.bbapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.bbapi.dao.TeaGoodsDao;
import com.qf.bbapi.dao.TeaTypeDao;
import com.qf.bbapi.dto.GoodInfoDto;
import com.qf.bbapi.dto.TypeGoodsDto;
import com.qf.bbapi.entity.TeaGoods;
import com.qf.bbapi.entity.TeaType;
import com.qf.bbapi.service.intf.TeaGoodsService;
import com.qf.bbapi.vo.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wodexinhuai
 * @create 2022-07-01-21:26
 */

@Service
public class TeaGoodsServiceImpl extends ServiceImpl<TeaGoodsDao, TeaGoods> implements TeaGoodsService {
    @Resource
    private TeaGoodsDao dao;
    @Resource
    private TeaTypeDao typeDao;

    @Override
    public R all() {
        List<TeaGoods> teaGoods = dao.selectList(null);
        if (teaGoods.size() > 0){
            //有商品
            return R.ok(teaGoods);
        }
        return R.fail("请添加商品");
    }

    @Override
    public R selByTid(int tid) {
        //参数校验
        if (tid > 0){
            List<TeaGoods> list = dao.selectList(new QueryWrapper<TeaGoods>().eq("tid", tid));

            if (list.size() > 0){
                return R.ok(list);
            }else {
                return R.fail("该类型无商品");
            }
        }
        return R.fail();
    }

    //根据typeId顺序查询
    @Override
    public R selToList() {
        //查询类型
        List<TypeGoodsDto> arrayList = new ArrayList<>();

        List<TeaType> list = typeDao.selectList(null);
        if (list.size() > 0){
            for (TeaType teaType : list) {
                TypeGoodsDto typeGoodsDto = new TypeGoodsDto();
                List<TeaGoods> teaGoodsList = dao.selectList(new QueryWrapper<TeaGoods>().eq("tid", teaType.getId()));
                typeGoodsDto.setGoodsList(teaGoodsList);
                typeGoodsDto.setTid(teaType.getId());
                arrayList.add(typeGoodsDto);
            }
            return R.ok(arrayList);
        }
        return R.fail();
    }

    @Override
    public R GoodIngo(int id) {
        if(id > 0){
            GoodInfoDto dto = dao.selOne(id);
            if (dto != null){
                return R.ok(dto);
            }
        }
        return R.fail("无此商品id");
    }
}
